package com.itheima.imclient100.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hyphenate.chat.EMConversation;
import com.itheima.imclient100.MainActivity;
import com.itheima.imclient100.R;
import com.itheima.imclient100.adapter.ConverstaionAdapter;
import com.itheima.imclient100.event.ContactChangedEvent;
import com.itheima.imclient100.event.MessageEvent;
import com.itheima.imclient100.presenter.ConversationPresenter;
import com.itheima.imclient100.presenter.impl.ConversationPresenterImpl;
import com.itheima.imclient100.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by fullcircle on 2017/7/2.
 *  这个是首页，就是和别人聊天的小窗口
 *
 */

public class ConversationFragment extends BaseFragment implements ConversationView {

    private RecyclerView rl_conversation;
    private FloatingActionButton fab;
    private ConverstaionAdapter adapter;
    private ConversationPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_conversation,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        rl_conversation = (RecyclerView) view.findViewById(R.id.recyclerview);
        fab = (FloatingActionButton) view.findViewById(R.id.fab);
        //清除掉所有已读的数据
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.clearAllUnReadMessageNumber();
                presenter.getConversation();
            }
        });

        rl_conversation.setLayoutManager(new LinearLayoutManager(getContext()));
        //adapter中的构造参数是能够传null的。
        adapter = new ConverstaionAdapter(null);
        rl_conversation.setAdapter(adapter);
        adapter.setListener(new ConverstaionAdapter.MyOnclickListener() {
            @Override
            public void onClick(String username) {
                presenter.markMessageAsReaded(username);
                Intent intent = new Intent(getContext(),ChatActivity.class);
                intent.putExtra("username",username);
                startActivity(intent);
            }

        });

        //数据都是从p层里面获取
        presenter = new ConversationPresenterImpl(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getConversation();
    }

    //关于adapter是可以放在p层也是把p层中的数据获取出来放在再v层让adapter自行获取（还是这个比较的方便，因为万一有很多个adapter呢？）
    @Override
    public void onGetConversation(List<EMConversation> conversations) {
        adapter.setConversationList(conversations);
        adapter.notifyDataSetChanged();
        //获取activity
        MainActivity activity = (MainActivity) getActivity();
        //调用设置气泡数量的方法
        activity.setNumberBadgeItem(presenter.getAllUnReadMessageNumber());
    }


    //（这个eventbus是在哪来的？？？？？？）
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveMsg(MessageEvent event){
        //收到消息之后 刷新界面 更新未读消息的数据
        presenter.getConversation();
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
